<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="80px"
      :inline="false"
      size="samll"
    >
      <el-form-item label="视频aid" prop="aid">
        <el-input v-model="form.aid"></el-input>
      </el-form-item>
      <el-form-item label="视频数据" prop="list">
        <el-input type="textarea" :row="10" v-model="form.list"></el-input>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import courseChapterApi from "@/api/courseChapter";
export default {
  name: "courseChapterSaveBatch",
  props: {
    courseId: {
      type: String,
      default: "",
    },
    close: {
      type: Boolean,
      defalut: false,
    },
  },
 
  data() {
    return {
      form: {
        aid: "",
        list:"",
      },
      rules: {
        aid: [{ required: true, message: "视频aid不能为空", trigger: "blur" }],
        list: [{ required: true, message: "视频数据数组不能为空", trigger: "blur" }],
      },
    };
  },
   watch: {
    close(newValue, oldValue) {
      this.form = {
        aid: "",
        list:"",
      };
    },
  },
  methods: {
    
    // 保存
    onSubmit() {
      this.$refs.form.validate((v) => {
        let aid = this.form.aid
        let list = JSON.parse(this.form.list)
        let finalList = []
        list.forEach((item,index)=>{
          let jsonData = {}
          jsonData.courseId = this.courseId
          jsonData.sort = index+1
          jsonData.name=item.part
          jsonData.url=`https://player.bilibili.com/player.html?aid=${aid}&cid=${item.cid}&page=${item.page}&as_wide=1&high_quality=1&danmaku=0`
          finalList.push(jsonData)
        })
        this.handleAdd(finalList)
      });
    },
    // 添加
    async handleAdd(list) {

      let res = await courseChapterApi.addBatch(list);
      if (res.success) {
        this.$message.success("添加成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },
    handleClose() {
      this.$emit("done");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>